//
//  Share.swift
//  DSP
//
//  Created by tucici on 2024/5/24.
//

import UIKit
import TCCKit
class Share: NSObject {
    class func share(_ model:ChannelManager){
        DispatchQueue.main.async {
            CLHUD(type: .loading, time: 100, callBack: nil)
        }
        let date = model.date.replacingOccurrences(of: " ", with: "-")
        let fileName = model.name + date
        let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
        let filePath = (documentPath as NSString).appendingPathComponent("/\(fileName).bin")
        
        do {
            try model.data.write(toFile: filePath, atomically: true, encoding: .utf8)
            share(at: filePath)
        } catch {
            let alert = UIAlertController(title: nil, message: "Share error \(error.localizedDescription)", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
            self.currentVC().present(alert, animated: true, completion: nil)
        }
        
        func share(at path: String){
            let fileURL = URL(fileURLWithPath: path)
            let activityViewController = UIActivityViewController(activityItems: [fileURL], applicationActivities: nil)
            //activityViewController.excludedActivityTypes = [.postToFacebook, .postToTwitter, .message]
            //iphone中为模式跳转
            self.currentVC().present(activityViewController, animated: true, completion: {
                CLHUDDismiss()
            })
        }
        
    }
}
